package com.easycarebn.main.view;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.data.DoctorScheduleResponse;
import com.easycarebn.request.data.DoctorScheduleResponse.TimeItem;
import com.timbacsi.main.R;

public class LineTimeBox {
	private static final int SIZE = 65;
	public final static int STT_EMPTY = -1;
	public final static int STT_WATT = 0;
	public final static int STT_CONFIRMED = 1;
	public final static int STT_COMPLATE = 2;

	private View mView;
	private String TAG_LINEBIG = "linebig";

	private String TAG_LINEBOOK = "linebook";
	private String TAG_TIME = "timebook";
	private String TAG_STT = "lblSttbook";

	private List<TimeItem> mList;

	private IResult callIResult;
	private IResult mICancel;

	private String mDate;
	private int mDocterId;
	private Activity mActivity;
	private DoctorScheduleResponse mData;

	public LineTimeBox(Activity activity, IResult callIResult,
			List<TimeItem> list, View view, int mDocterId,DoctorScheduleResponse data, IResult iCancel) {
		this.mActivity = activity;
		this.mView = view;
		this.mList = list;
		this.mDocterId = mDocterId;
		this.callIResult = callIResult;
		this.mData=data;
		this.mICancel = iCancel;
	}

	public void setList(List<TimeItem> mList) {
		this.mList = mList;
		Log.d("mList", mList.size() + "");
	}

	public void goneLine() {

	}

	public void init(String date) {
		// goneAllItem();
		// goneLine();
		mDate = date;
		setData();
	}

	public void goneAllItem() {

		for (int i = 0; i < SIZE / 5; i++) {
			Log.d("lineBox", "lineBox" + i);
			LinearLayout lineBox = (LinearLayout) mView
					.findViewWithTag(TAG_LINEBIG + i);
			lineBox.setVisibility(View.VISIBLE);
		}

		for (int i = 0; i < SIZE; i++) {
			LinearLayout box = (LinearLayout) mView
					.findViewWithTag(TAG_LINEBOOK + i);
			// timebox.setVisibility(View.GONE);
			box.setBackgroundColor(Color.parseColor("#ffffff"));

			TextView bookStt = (TextView) mView.findViewWithTag(TAG_STT + i);
			TextView time = (TextView) mView.findViewWithTag(TAG_TIME + i);
			time.setText("");
			bookStt.setVisibility(View.VISIBLE);
			bookStt.setText("");
		}

		int line = mList.size() / 5;
		if (mList.size() % 5 > 0) {
			line++;
		}
		for (int i = line; i < SIZE / 5; i++) {
			Log.d("lineBox", "lineBox" + i);
			LinearLayout lineBox = (LinearLayout) mView
					.findViewWithTag(TAG_LINEBIG + i);
			lineBox.setVisibility(View.GONE);
		}

		for (int i = mList.size(); i < SIZE; i++) {
			TextView bookStt = (TextView) mView.findViewWithTag(TAG_STT + i);
			bookStt.setVisibility(View.GONE);
		}

		/*
		 * int du = mList.size() % 5; int nguyen = mList.size() / 5; int
		 * firtGone = mList.size(); if (du > 0) { firtGone = (nguyen * 5) + 5; }
		 * Log.d("firtGone", firtGone + "");
		 * 
		 * for (int i = firtGone + 1; i < SIZE; i++) { LinearLayout timebox =
		 * (LinearLayout) mView.findViewWithTag(TAG_LINEBOOK + i);
		 * timebox.setVisibility(View.GONE);
		 * 
		 * }
		 */

	}

	public void setBackgroundColor(LinearLayout timebox, String status,
			TextView lblTime) {
		int code = STT_EMPTY;
		if (TextUtils.isEmpty(status) || "".equals(status)) {
			code = STT_EMPTY;
		} else {
			code = Integer.valueOf(status);
		}

		switch (code) {
		case STT_EMPTY:
			timebox.setBackgroundColor(Color.parseColor("#3694eb"));
			lblTime.setTextColor(Color.parseColor("#ffffff"));
			break;
		case STT_WATT:
			timebox.setBackgroundColor(Color.parseColor("#ffd646"));
			lblTime.setTextColor(Color.parseColor("#000000"));
			break;

		case STT_COMPLATE:
			timebox.setBackgroundColor(Color.parseColor("#c2c2c2"));
			lblTime.setTextColor(Color.parseColor("#000000"));
			break;
		case STT_CONFIRMED:
			timebox.setBackgroundColor(Color.parseColor("#f14444"));
			lblTime.setTextColor(Color.parseColor("#000000"));
			break;

		}
	}

	public void setData() {
		goneAllItem();
		for (int i = 0; i < mList.size() && i < SIZE; i++) {
			TextView tvstt = (TextView) mView.findViewWithTag(TAG_STT + i);
			tvstt.setVisibility(View.VISIBLE);

			LinearLayout timebox = (LinearLayout) mView
					.findViewWithTag(TAG_LINEBOOK + i);
			final TimeItem item = mList.get(i);
			final String datetime = mDate + "#" + mList.get(i).getBlock() + "#"
					+ mDocterId + "#" + mList.get(i).getAddress_id()+"#"+mList.get(i).getAddress_name()+"#"+mData.getShowdate();
			timebox.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (item.getStatus().isEmpty()
							|| item.getStatus().equals("null")) {
						callIResult.onDisplay(Utils.SELECT_TIME_BOOKING,
								datetime);
						if (mICancel != null)
							mICancel.onDisplay(Activity.RESULT_CANCELED, "");
					} else {
						Utils.showMessage(mActivity, mActivity.getResources()
								.getString(R.string.chon_o_xanh));
					}
				}
			});

			TextView time = (TextView) mView.findViewWithTag(TAG_TIME + i);
			time.setText(mList.get(i).getBlock());
			setBackgroundColor(timebox, mList.get(i).getStatus(), time);
			TextView stt = (TextView) mView.findViewWithTag(TAG_STT + i);
			stt.setText((i + 1) + "");

		}
	}
}
